home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / STDLIB.PAK / PARTSORT.CPP < prev    next >
C/C++ Source or Header  |  1997-05-06  |  1KB  |  48 lines

  1.  #include <vector>
  2.  #include <algorithm>
  3.  
  4.  using namespace std;
  5.  
  6.  int main()
  7.  {
  8.    int d1[20] = {17, 3,  5,  -4, 1, 12, -10, -1, 14, 7,
  9.                  -6, 8, 15, -11, 2, -2,  18,  4, -3, 0};
  10.    //
  11.    // Set up a vector.
  12.    //
  13.    vector<int> v1(d1+0, d1+20);
  14.    //
  15.    // Output original vector.
  16.    //
  17.    cout << "For the vector: ";
  18.    copy(v1.begin(), v1.end(), ostream_iterator<int>(cout," "));
  19.    //
  20.    // Partial sort the first seven elements.
  21.    //
  22.    partial_sort(v1.begin(), v1.begin()+7, v1.end());
  23.    //
  24.    // Output result.
  25.    //
  26.    cout << endl << endl << "A partial_sort of seven elements gives: "
  27.         << endl << "     ";
  28.    copy(v1.begin(), v1.end(), ostream_iterator<int>(cout," "));
  29.    cout << endl;
  30.    //
  31.    // A vector of ten elements.
  32.    //
  33.    vector<int> v2(10, 0);
  34.    //
  35.    // Sort the last ten elements in v1 into v2.
  36.    //
  37.    partial_sort_copy(v1.begin()+10, v1.end(), v2.begin(), v2.end());
  38.    //
  39.    // Output result.
  40.    //
  41.    cout << endl << "A partial_sort_copy of the last ten elements gives: "
  42.         << endl << "     ";
  43.    copy(v2.begin(), v2.end(), ostream_iterator<int>(cout," "));
  44.    cout << endl;
  45.  
  46.    return 0;
  47.  }
  48.